package com.bringway.p2p.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.fm.controller.AttController;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.TxService;

/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月16日 上午9:34:42
 * @功能：提取现金界面
 */
@Controller
@RequestMapping("/tx")
public class TxController extends JsonController{
	
	Logger logger = Logger.getLogger(TxController.class);

	@Autowired
	private TxService txService;
	
	/**
	 * 
	 * @param request  提现查询
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryTxList")
	public ModelAndView queryTxList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String gyh = RequestUtils.getStringParameter(request, "gyh");
		String txState = RequestUtils.getStringParameter(request, "txState");
		/**
		 * 2017-02-10
		 * 新增处理日期查询
		 * */
		String operStartDate = RequestUtils.getStringParameter(request, "operStartDate");
		String operEndDate = RequestUtils.getStringParameter(request, "operEndDate");
		
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("sjhm", sjhm);
		params.put("gyh", gyh);
		params.put("txState", txState);
		params.put("operStartDate", operStartDate);
		params.put("operEndDate", operEndDate);
		
		List<Map<String, Object>> list = txService.queryTxList(params);
		String SumJe = txService.getTxSum(params);
		int count = txService.getTxCount(params);
		result.put("data", list);	
		result.put("total", count);
		result.put("SumJe", SumJe);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("GYH", "体现合计:");//key是字段名
			map.put("TXJE", SumJe);//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request,response,result);
		return null;
	}
	
	/**
	 * 提现导出列表信息
	 * */
	@RequestMapping("exportTxExcelMethod")
	public void exportTxExcelMethod(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String gyh = RequestUtils.getStringParameter(request, "gyh");
		String txState = RequestUtils.getStringParameter(request, "txState");
		/**
		 * 2017-02-10
		 * 新增处理日期查询
		 * */
		String operStartDate = RequestUtils.getStringParameter(request, "operStartDate");
		String operEndDate = RequestUtils.getStringParameter(request, "operEndDate");
		JSONObject params = new JSONObject();
		
		params.put("queryType", "export");
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("sjhm", sjhm);
		params.put("gyh", gyh);
		params.put("txState", txState);
		params.put("operStartDate", operStartDate);
		params.put("operEndDate", operEndDate);
		// 导出页面数据
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		List<Map<String, Object>>  txList = this.txService.queryTxList(params);
		
		sheetList = this.txService.exportTxData(txList);
		// 带Format的Excel导出
		ExcelOperUtil util = new ExcelOperUtil();
		// 获取template相对路径cn.com.worklog.
		InputStream stream = AttController.class.getResourceAsStream("/com/bringway/p2p/config/tx.xls");
		OutputStream os = response.getOutputStream();
		super.setDownLoadResponseHeader(request, response, "提现.xls");
		util.writeToExcelByStencil(stream, sheetList, os);
	}
	
	/**
	 * 处理提现
	 * */
	@RequestMapping("updateTxState")
	public void updateTxState(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ids = RequestUtils.getStringParameter(request, "ids");
		String operState = RequestUtils.getStringParameter(request, "operState");
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		params.put("ids", ids);
		params.put("operState", operState);
		try{
			String res = txService.updateTxState(params);
			if("success".equals(res)){
				result.put("success", true);
				result.put("isDelSuc", true);
				result.put("msg", "处理成功！");
			}else if("res_01".equals(res)){
				result.put("success", true);
				result.put("isDelSuc", false);
				result.put("msg", "您的操作有误！");
			}else if("res_02".equals(res)){
				result.put("success", true);
				result.put("isDelSuc", false);
				result.put("msg", "处理类型异常！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
			result.put("success", false);
			result.put("msg", "数据异常！");
		}
		serialize(request, response, result);
	}
	
}
